package com.upb.webserver.role;

import com.upb.webserver.common.constant.Constant;
import com.upb.webserver.common.enums.PlatformEnum;
import com.upb.webserver.common.exception.WebServerException;
import com.upb.webserver.common.returncode.ConstantCode;
import com.upb.webserver.common.tools.JsonUtils;
import com.upb.webserver.dao.entity.TbRole;
import com.upb.webserver.dao.entity.TbRoleExample;
import com.upb.webserver.dao.mapper.TbRoleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class RoleService {
    @Autowired
    private TbRoleMapper roleMapper;


    /**
     * @param platform
     * @return
     */
    public List<TbRole> listRoleVoByPlatform(byte platform) {
        log.info("start exec method [listRoleVoByPlatform]. platform:{}", platform);

        List<String> developerRoleNameList = new ArrayList<>();

        TbRoleExample roleExample = new TbRoleExample();
        TbRoleExample.Criteria criteria = roleExample.createCriteria();
        if (PlatformEnum.DEVELOPER.getId() == platform) {
            criteria.andNameIn(Constant.ROLE_NAME_LIST_DEVELOPER);
        } else {
            criteria.andNameNotIn(Constant.ROLE_NAME_LIST_DEVELOPER);
        }
        return roleMapper.selectByExample(roleExample);
    }


    /**
     * @param platform
     * @return
     */
    public Set<Integer> getAllRoleIdByPlatform(byte platform) {
        log.info("start exec method [getAllRoleIdByPlatform]. platform:{}", platform);

        TbRoleExample example = new TbRoleExample();
        TbRoleExample.Criteria criteria = example.createCriteria();

        if (PlatformEnum.DEVELOPER.getId() == platform) {
            criteria.andNameIn(Constant.ROLE_NAME_LIST_DEVELOPER);
        } else if (PlatformEnum.DEV_OPS.getId() == platform) {
            criteria.andNameIn(Constant.ROLE_NAME_LIST_DEV_OPS);
        } else if (PlatformEnum.AUDIT.getId() == platform) {
            criteria.andNameIn(Constant.ROLE_NAME_LIST_AUDIT);
//        } else if (PlatformEnum.API.getId() == platform) {
//            criteria.andNameIn(Constant.ROLE_NAME_LIST_API);
        }
        else {
            log.warn("fail exec method[getAllRoleIdByPlatform]. not support platform:{}", platform);
            throw new WebServerException(ConstantCode.PLATFORM_NOT_SUPPORT);
        }

        if (roleMapper.countByExample(example) == 0) {
            log.warn("fail exec method[getAllRoleIdByPlatform]. not found record by platform:{}", platform);
            throw new WebServerException(ConstantCode.PLATFORM_NOT_SUPPORT);
        }
        List<TbRole> roleList = roleMapper.selectByExample(example);
        Set<Integer> roleIdSet = roleList.stream().map(role -> role.getId()).collect(Collectors.toSet());
        log.warn("success exec method[getAllRoleIdByPlatform]. result:{}", JsonUtils.objToString(roleIdSet));
        return roleIdSet;
    }

}
